package com.hyh.dao.impl;

import com.hyh.bean.Menu;
import com.hyh.bean.Role;
import com.hyh.bean.Users;
import com.hyh.dao.DBUtils;
import com.hyh.dao.RoleDao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class RoleDaoImpl extends DBUtils implements RoleDao {
    @Override
    public List<Role> getRoles(int pageIndex,int pageSize) {
        List<Role> roleList = new ArrayList<Role>();
        try {
            String sql = "select * from role limit ?,?";
            List params = new ArrayList();
            params.add((pageIndex - 1) * pageSize);
            params.add(pageSize);
            resultSet = query(sql, params);
            while(resultSet.next()){

                Role role = new Role();
                role.setRoleId(resultSet.getInt("roleid"));
                role.setRoleName(resultSet.getString("rolename"));
                role.setRoleState(resultSet.getInt("rolestate"));
                roleList.add(role);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeAll();
        }
        return roleList;
    }

    @Override
    public int total() {
        int totals = 0;
        try {
            String sql = "select count(1) from role ";
            resultSet = query(sql, null);
            while(resultSet.next()){
                totals = resultSet.getInt(1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeAll();
        }
        return totals;
    }

    @Override
    public int insert(Role role) {
        int i = 0;//新增数据的id
        try {
            String sql = "insert into role values(null,?,?)";
            List params = new ArrayList();
            params.add(role.getRoleName());
            params.add(role.getRoleState());
            int update = update(sql, params);
            ResultSet generatedKeys = pps.getGeneratedKeys();
            while (generatedKeys.next()){
                i = generatedKeys.getInt(1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        return i;
    }

    @Override
    public Role findById(int rid) {
        Role role = new Role();
        List list = new ArrayList();
        try {
            String sql = "select * from role r,middle m where r.roleid=m.roleid and r.roleid=?";
            List params = new ArrayList();
            params.add(rid);
            resultSet = query(sql, params);
            while (resultSet.next()){
                role.setRoleId(resultSet.getInt("roleid"));
                role.setRoleName(resultSet.getString("rolename"));
                role.setRoleState(resultSet.getInt("rolestate"));
                Menu menu = new Menu();
                menu.setMenuId(resultSet.getInt("menuid"));
                list.add(menu);
                role.setMenuList(list);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeAll();
        }
        return role;
    }

    @Override
    public int update(Role role) {
        int update = 0;
        try {
            String sql = "update role set rolename=?,rolestate=? where roleid=?";
            List params = new ArrayList();
            params.add(role.getRoleName());
            params.add(role.getRoleState());
            params.add(role.getRoleId());
            update = update(sql, params);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }

        return update;
    }

    @Override
    public int delete(int rid) {
        int del = 0;
        try {
            String sql = "delete from role where roleid=?";
            List params = new ArrayList();
            params.add(rid);
            del = update(sql, params);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        return del;
    }

    @Override
    public int start(int rid) {
        int k = 0;
        try {
            String sql = "update role set rolestate=1 where roleid=?";
            List parsms = new ArrayList();
            parsms.add(rid);
            k = update(sql,parsms);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }

        return k;
    }

    @Override
    public Role findRoleId(int roleid) {
        Role role = new Role();
        List menuList = new ArrayList();
        try {
            String sql = "select * from role r,menu m,middle mid where r.roleid=mid.roleid and mid.menuid=m.menuid and r.roleid=?";
            List params = new ArrayList();
            params.add(roleid);
            resultSet = query(sql,params);
            while (resultSet.next()){
                role.setRoleId(resultSet.getInt("roleid"));
                role.setRoleName(resultSet.getString("rolename"));
                role.setRoleState(resultSet.getInt("rolestate"));
                Menu menu = new Menu();
                menu.setMenuId(resultSet.getInt("menuid"));
                menu.setMenuName(resultSet.getString("menuname"));
                menu.setUrl(resultSet.getString("url"));
                menu.setState(resultSet.getInt("state"));
                menu.setUpMenuId(resultSet.getInt("upmenuid"));
                menuList.add(menu);
            }
            role.setMenuList(menuList);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeAll();
        }
        return role;
    }
}
